package com.goldcn.oss.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.goldcn.common.util.UuidUtils;
import com.goldcn.config.OSSConfigure;
import com.mongodb.gridfs.GridFSDBFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * Created by Administrator on 2016/8/12.
 */
public class OSSManageUtil {


    /**
     * 获取阿里云OSS客户端对象
     *
     * @param ossConfigure 配置文件
     * @return
     */
    public static final OSSClient getOSSClient(OSSConfigure ossConfigure) {
        return new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(),
                ossConfigure.getAccessKeySecret());
    }


    /**
     * 上传OSS服务器文件
     *
     * @param @param  ossConfigure
     * @param @param  file
     * @param @param  remotePath
     * @param @throws Exception    设定文件
     * @return String    返回类型
     * @throws
     * @Title: uploadFile
     * @Description:
     */
    public static String uploadFile(OSSClient ossClient, String bucketName, MultipartFile file) throws Exception {

        InputStream fileContent = file.getInputStream();

        //生成新的文件名
        String fileName = UuidUtils.uuid() + file.getOriginalFilename().substring(
                file.getOriginalFilename().lastIndexOf("."));

        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(fileContent.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //上传文件
        ossClient.putObject(bucketName, fileName, fileContent);
        ossClient.shutdown();
        return fileName;
    }

    /**
     * 上传OSS服务器文件
     *
     * @param @param  ossConfigure
     * @param @param  file
     * @param @param  remotePath
     * @param @throws Exception    设定文件
     * @return String    返回类型
     * @throws
     * @Title: uploadFile
     * @Description:
     */
    public static String uploadFile(OSSClient ossClient, String bucketName, InputStream file, String fileName) throws Exception {

        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
//        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //上传文件
        ossClient.putObject(bucketName, fileName, file);
        ossClient.shutdown();
        return fileName;
    }


    /**
     * 上传OSS服务器文件
     *
     * @param @param  ossConfigure
     * @param @param  file
     * @param @param  remotePath
     * @param @throws Exception    设定文件
     * @return String    返回类型
     * @throws
     * @Title: uploadFile
     * @Description:
     */
    public static String uploadFile(OSSClient ossClient, String bucketName, byte[] data, String fileName) throws Exception {

        InputStream file = new ByteArrayInputStream(data);
        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //上传文件
        ossClient.putObject(bucketName, fileName, file);
        ossClient.shutdown();
        return fileName;
    }

    /**
     * 上传OSS服务器文件
     *
     * @param @param  ossConfigure
     * @param @param  file
     * @param @param  remotePath
     * @param @throws Exception    设定文件
     * @return String    返回类型
     * @throws
     * @Title: uploadFile
     * @Description:
     */
    public static String uploadFile(OSSClient ossClient, String bucketName, GridFSDBFile file) throws Exception {

        InputStream fileContent = file.getInputStream();

        //生成新的文件名
        String fileName = file.getFilename();

        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(fileContent.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //上传文件
        ossClient.putObject(bucketName, fileName, fileContent);
        ossClient.shutdown();
        return fileName;
    }

    public static OSSObject downloadFile(OSSClient ossClient, String bucketName, String fileName) throws Exception {
        OSSObject ossObject = ossClient.getObject(bucketName, fileName);
        return ossObject;
    }

    /**
     * 根据key删除OSS服务器上的文件
     *
     * @param @param ossConfigure
     * @param @param filePath    设定文件
     * @return void    返回类型
     * @throws
     * @Title: deleteFile
     * @Description:
     */
    public static void deleteFile(OSSClient ossClient, String bucketName, String filePath) {
        ossClient.deleteObject(bucketName, filePath);
    }


    /**
     * 关闭阿里云OSS客户端对象
     *
     * @param ossClient
     */
    public static final void closeOSSClient(OSSClient ossClient) {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}
